package org.dromara.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.ProductTable;
import org.dromara.business.domain.bo.ProductTableBo;
import org.dromara.business.domain.vo.ProductTableVo;
import org.dromara.business.mapper.ProductTableMapper;
import org.dromara.business.service.BusinessConfigService;
import org.dromara.business.service.ProductTableService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 产品Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-22
 */
@RequiredArgsConstructor
@Service
public class ProductTableServiceImpl extends ServiceImpl<ProductTableMapper,ProductTable> implements ProductTableService {

    private final ProductTableMapper baseMapper;

    private final BusinessConfigService businessConfigService;

    /**
     * 查询产品
     */
    @Override
    public ProductTableVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询产品列表
     */
    @Override
    public TableDataInfo<ProductTableVo> queryPageList(ProductTableBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductTable> lqw = buildQueryWrapper(bo);
        Page<ProductTableVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品列表
     */
    @Override
    public List<ProductTableVo> queryList(ProductTableBo bo) {
        LambdaQueryWrapper<ProductTable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductTable> buildQueryWrapper(ProductTableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductTable> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getProductName()), ProductTable::getProductName, bo.getProductName());
        lqw.like(bo.getProductId()!=null, ProductTable::getProductId, bo.getProductId());
        lqw.like(StrUtil.isNotBlank(bo.getProductIdTwo()), ProductTable::getProductIdTwo,bo.getProductIdTwo());
        lqw.eq(bo.getPrice() != null, ProductTable::getPrice, bo.getPrice());
        lqw.eq(bo.getNumber() != null, ProductTable::getNumber, bo.getNumber());
        lqw.orderByDesc(ProductTable::getCreateTime);
        return lqw;
    }

    /**
     * 新增产品
     */
    @Override
    public Boolean insertByBo(ProductTableBo bo) {
        ProductTable add = MapstructUtils.convert(bo, ProductTable.class);
        ProductTable one = this.lambdaQuery().eq(ProductTable::getProductIdTwo, bo.getProductIdTwo()).one();
        if(one!=null){
            throw new ServiceException("id重复 请重试");
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改产品
     */
    @Override
    public Boolean updateByBo(ProductTableBo bo) {
        ProductTable update = MapstructUtils.convert(bo, ProductTable.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductTable entity){

    }

    /**
     * 批量删除产品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ProductTable> getProductTableByIds() {
        //获取阈值
        String prokey = businessConfigService.queryValueByKey("Prokey");
        //新建查询条件
        QueryWrapper<ProductTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(ProductTable::getNumber, prokey);
        List<ProductTable> productTables = baseMapper.selectList(queryWrapper);
        return productTables;
    }

    @Override
    public String getProductName(Long id) {
        LambdaQueryWrapper<ProductTable> wrapper = new LambdaQueryWrapper<ProductTable>()
            .eq(ProductTable::getProductIdTwo, id);
        String productName = baseMapper.selectOne(wrapper).getProductName();
        return productName;
    }
}
